package com.newhero.product.common.scan;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.newhero.product.common.util.GTime;
import com.newhero.product.common.util.StringUtils;
import com.newhero.product.module.sys.model.Mlog;
import com.newhero.product.module.sys.service.IMlogService;

/**
 * @ClassName: SysLogAspect 
 * @Description: 系统拦截日志AOP 
 * @author Caolj 648198687@qq.com 
 * @date 2017-4-5 下午5:07:20
 */
@Aspect
@Component
public class SysLogAspect {
	private static final Logger LOGGER = LogManager
			.getLogger(SysLogAspect.class);

	@Autowired
	private IMlogService mlogServiceImpl;

	@Pointcut("within(@org.springframework.stereotype.Controller *)")
	public void cutController() {
	}

	@Around("cutController()")
	public Object recordSysLog(ProceedingJoinPoint point) throws Throwable {
		String strMethodName = point.getSignature().getName();
		String strClassName = point.getTarget().getClass().getName();
		Object[] params = point.getArgs();
		StringBuffer bfParams = new StringBuffer();
		Enumeration<String> paraNames = null;
		HttpServletRequest request = null;
		if (params != null && params.length > 0) {
			request = ((ServletRequestAttributes) RequestContextHolder
					.getRequestAttributes()).getRequest();
			paraNames = request.getParameterNames();
			String key;
			String value;
			while (paraNames.hasMoreElements()) {
				key = paraNames.nextElement();
				value = request.getParameter(key);
				bfParams.append(key).append("=").append(value).append("&");
			}
			if (StringUtils.isBlank(bfParams)) {
				bfParams.append(request.getQueryString());
			}
		}

		String strMessage = String.format("%s.%s", strClassName, strMethodName);
//		LOGGER.info(strMessage + "[参数]:" + bfParams);
//		LOGGER.info("请求参数为：" + bfParams);
		try {
			Subject currentUser = SecurityUtils.getSubject();
			PrincipalCollection collection = currentUser.getPrincipals();
			if (null != collection) {
				String loginName = collection.getPrimaryPrincipal().toString();
				Mlog sysLog = new Mlog();
				sysLog.setUserID(loginName);
				sysLog.setSiteID("");
				sysLog.setMlogContent(strMessage);
				sysLog.setMlogParam(bfParams.toString());
				sysLog.setMlogTime(GTime.getNormalTime());
				if (request != null) {
					sysLog.setMlogIP(request.getRemoteAddr());
				}
				mlogServiceImpl.insert(sysLog);
			}
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}

		return point.proceed();
	}

}
